package yxy.game.pm2.module.infinite;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import yxy.apple.logger.Logger;
import yxy.apple.util.concurrent.ThreadPoolManager;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.infiniterankreward;
import yxy.cherry.data.string.GameString;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.module.AreaModule;
import yxy.game.pm2.bean.module.rank.PlayerRankModule;
import yxy.game.pm2.bean.module.rank.RankEntry;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.module.mail.MailHandle;
import yxy.game.pm2.module.mail.MailTypeEnum;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

//神将试炼 排行榜
public class RankInfinite extends PlayerRankModule {

	public RankInfinite(AreaBean areaBean) {
		super(areaBean);
	}

	@Override
	public String rankName() {
		return PlayerRankModule.getAllRankNames().get(4);
	}

	@Override
	protected long valuePlastic(long value) {
		return value * 100;
	}

	@Override
	protected long valueRestore(long score) {
		return score / 100;
	}

	@Override
	public void handleUpdate(DateTime ori, DateTime now) {
		if (new LocalDate(now).equals(new LocalDate(ori))) {
			return;
		}
		boolean weekUpdate = new LocalDate(now).getWeekOfWeekyear() != new LocalDate(ori).getWeekOfWeekyear();

		ArrayList<RankEntry> dailyTop = dailyTop();
		if (weekUpdate) {
			clear();
		}
		rewardDayRank(dailyTop);

//		ThreadPoolManager.getInstance().execute(new Runnable() {
//			@Override
//			public void run() {
//				ArrayList<RankEntry> dailyTop = dailyTop();
//				if (weekUpdate) {
//					clear();
//				}
//				rewardDayRank(dailyTop);
//			}
//		});
	}

	private ArrayList<RankEntry> dailyTop() {
		int min = -1;// 全局最小
		int max = -1;// 全局最大
		Map<String, infiniterankreward> all = DataCenter.getAllData(infiniterankreward.class);
		for (infiniterankreward data : all.values()) {
			if (min < 0 || data.getRankingmin() < min) {
				min = data.getRankingmin();
			}
			if (max < 0 || data.getRankingmax() > max) {
				max = data.getRankingmax();
			}
		}
		return new ArrayList<>(range(min, max));
	}

	private void rewardDayRank(ArrayList<RankEntry> dailyTop) {
		System.out.println("区服" + getAreaId() + ":神将试炼发排名奖励了！！！！" + DateTime.now().toString());
		Map<String, infiniterankreward> all = DataCenter.getAllData(infiniterankreward.class);
		for (infiniterankreward data : all.values()) {
			Collection<Currency> rewards = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());

			for (int i = data.getRankingmin() - 1; i < data.getRankingmax() && i < dailyTop.size(); i++) {
				int rank = i + 1;
				RankEntry entry = dailyTop.get(i);
				String title = GameString.神将试炼排名奖励_title(rank).getText();
				String content = GameString.神将试炼排名奖励_msg(rank).getText();
				new MailHandle().sendMail(entry.getKey(), MailTypeEnum.Mail, title, content, CCReason.神将试炼, rewards);
			}
		}
		System.out.println("区服" + getAreaId() + ":神将试炼发排名奖励发完了" + DateTime.now().toString());
	}

	static public RankInfinite getRank(String playerCode) {
		AreaBean areaBean = AreaModule.area(playerCode);
		return new RankInfinite(areaBean);
	}
}
